//导包
importClass(android.graphics.PorterDuffXfermode) //导入安卓PorterDuff画笔设置类

//变量创建
//定义悬浮窗、画布
var win = floaty.rawWindow(
    <frame gravity="center">
        <canvas id="board" w="auto" h="auto" />
    </frame>
);
//定义画笔
var paint = new Paint();
//定义路径
var path = new android.graphics.Path()
//设置画笔和悬浮窗
win.setTouchable(false);//悬浮窗不可触摸
win.setSize(-1, -1);//-1为铺满屏幕
paint.setColor(colors.parseColor("#feeeed"));//樱花色画笔
paint.setStyle(Paint.Style.FILL);//设置画笔为填充模式
//创建花瓣列表
var position = []
for (n = 0; n < 99; n++) {
    if (n == 0) {
        position.push([Math.round(Math.random() * device.width), Math.round(Math.random() * device.height / 7)])
    } else {
        while (true) {
            x = Math.floor(Math.random() * device.width);
            y = Math.floor(Math.random() * (device.height / 4 - 3 + 1) + 3);
            if (position.indexOf([x, y]) > -1) {
                continue;
            } else {
                position.push([x, y])
                break;
            }
        }
    }
}

//监听作画动作，绘制图像
win.board.on("draw", (canvas) => {
    //橡皮清空画布
    xfermode = new PorterDuffXfermode(android.graphics.PorterDuff.Mode.CLEAR);
    paint.setXfermode(xfermode);
    canvas.drawPaint(paint);
    paint.setXfermode(null);
    canvas.drawColor(colors.parseColor("#00000000"));
    for (n = 0; n < position.length; n++) {
        x = position[n][0];
        y = position[n][1];
        if (x == undefined) {
            x = 0;
        }
        if (y == undefined) {
            y = 0;
        }
        canvas.rotate(37.2);//旋转37.2度
        path.reset()//清空路径
        path.moveTo(x + 9, y - 3)//贝塞尔曲线起始点
        //贝塞尔曲线控制点，终点,终点为下一个抛物线起点
        path.quadTo(x, y, x, y + 9)
        path.quadTo(x, y + 33, x + 15, y + 39)
        path.quadTo(x + 30, y + 33, x + 30, y + 9)
        //贝塞尔曲线控制点，终点,终点为下一个线段起点
        path.quadTo(x + 30, y, x + 21, y - 3)
        //线段，终点，终点为下一个线段起点
        path.lineTo(x + 15, y + 3)
        path.lineTo(x + 9, y - 3)
        canvas.drawPath(path, paint)
    }
})
//创建定时器，每17ms触发一次间隔
threads.start(function () {
    setInterval(() => {
        position = position.map(n => n.map(p => p >= 1.2*device.width || p >= 1.5*device.height ? p = 0 : p += 1));
    }, 17);
})
